package com.mzy.NetworkProgram.networkinterfacebasic.data;

import lombok.Getter;
import lombok.Setter;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/4/24 10:14
 * @Version 1.0
 */
@Getter
@Setter
public class ApiResult<T> {
    private int code = 200;
    private String title;
    private String describe;
    private T data;


    public static final class ApiResultBuilder {
        private int code = 200;
        private String title;
        private String describe;
        private Object data;


        public ApiResultBuilder withCode(int code) {
            this.code = code;
            return this;
        }

        public ApiResultBuilder withTitle(String title) {
            this.title = title;
            return this;
        }

        public ApiResultBuilder withDescribe(String describe) {
            this.describe = describe;
            return this;
        }

        public ApiResultBuilder withData(Object data) {
            this.data = data;
            return this;
        }

        public <T> ApiResult<T> build() {
            ApiResult<T> result = new ApiResult<>();
            result.setCode(code);
            result.setTitle(title);
            result.setDescribe(describe);
            result.setData((T) data);
            return result;
        }

        public <T> ApiResult<T> buildError() {
            ApiResult<T> result = new ApiResult<>();
            result.setCode(500);
            result.setTitle(title);
            result.setDescribe(describe);
            result.setData((T) data);
            return result;
        }

        public <T> ApiResult<T> buildOk() {
            ApiResult<T> result = new ApiResult<>();
            result.setCode(200);
            result.setTitle(title);
            result.setDescribe(describe);
            result.setData((T) data);
            return result;
        }
    }

    public static ApiResultBuilder createBuilder() {
        return new ApiResultBuilder();
    }
}
